unit elements;
(*  cssMonkey
    Copyright (C) 2011 Matthew Hipkin <http://www.matthewhipkin.co.uk>

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, Menus, Buttons, miscfunc;

type

  { TfrmElement }

  TfrmElement = class(TForm)
    btnCancel: TButton;
    btnOK: TButton;
    checkShow: TCheckBox;
    dialogColour: TColorDialog;
    btnInsertColour: TSpeedButton;
    btnClear: TSpeedButton;
    btnDone: TSpeedButton;
    Label3: TLabel;
    Label4: TLabel;
    textValues: TComboBox;
    textAttributes: TComboBox;
    listAttributes: TListView;
    menuAttributes: TPopupMenu;
    btnDeleteAttribute: TSpeedButton;
    textElementName: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    procedure btnCancelClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure btnDeleteAttributeClick(Sender: TObject);
    procedure btnDoneClick(Sender: TObject);
    procedure btnInsertColourClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure listAttributesClick(Sender: TObject);
    procedure textAttributesEditingDone(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    elements: TStrings;
    tags: TStrings;
  end;

var
  frmElement: TfrmElement;

implementation

{$R *.lfm}

{ TfrmElement }

procedure TfrmElement.FormShow(Sender: TObject);
var
  x: integer;
  t: String;
begin
  // For some reason Lazarus doesn't like these set at design time
  listAttributes.Column[0].Width := 112;
  listAttributes.Column[1].Width := 112;
  // Populate the tags dropdown
  textElementName.Items.Clear;
  textElementName.Items.AddStrings(tags);
  // Populate the elements dropdown
  textAttributes.Items.Clear;
  textValues.Items.Clear;
  for x := 0 to elements.Count -1 do
  begin
    t := Copy(elements[x],1,Pos(':',elements[x])-1);
    textAttributes.Items.Add(t);
  end;
end;

procedure TfrmElement.listAttributesClick(Sender: TObject);
var
  l: TListItem;
begin
  if listAttributes.ItemIndex < 0 then exit;
  l := listAttributes.Items[listAttributes.ItemIndex];
  textAttributes.Text := l.Caption;
  textValues.Text := l.SubItems[0];
end;

{ MESSY MESSY! NEEDS TIDYING UP BUT WORKS FOR NOW }

procedure TfrmElement.textAttributesEditingDone(Sender: TObject);
var
  x,y: integer;
  key: String;
  value: String;
  attributes: TArray;
begin
  key := '';
  value := '';
  for x := 0 to elements.Count -1 do
  begin
    key := Copy(elements[x],1,Pos(':',elements[x])-1);
    value := Copy(elements[x],Pos(':',elements[x])+1,Length(elements[x]));
    if key = textAttributes.Text then
    begin
      textValues.Items.Clear;
      attributes := explode(';',value,0);
      for y := 0 to High(attributes) do textValues.Items.Add(attributes[y]);
      break;
    end; // end if
  end; // end for x
end;

procedure TfrmElement.FormCreate(Sender: TObject);
begin
  elements := TStringList.Create;
  tags := TStringList.Create;
end;

procedure TfrmElement.btnInsertColourClick(Sender: TObject);
begin
  if dialogColour.Execute then
  begin
    textValues.Text := ColourToHTML(dialogColour.Color);
  end;
end;

procedure TfrmElement.btnOKClick(Sender: TObject);
var
  continue: Boolean;
begin
  continue := true;
  if listAttributes.Items.Count = 0 then
    if MessageDlg('This tag contains no elements, are you sure you wish to add it?',mtInformation, mbOKCancel, 0) = mrCancel then continue := false;
  if continue then ModalResult := mrOK;
end;

procedure TfrmElement.btnDoneClick(Sender: TObject);
var
  l: TListItem;
begin
  if textAttributes.Text = '' then exit;
  if listAttributes.ItemIndex < 0 then
  begin
    l := listAttributes.Items.Add;
    l.Caption:=textAttributes.Text;
    l.SubItems.Add(textValues.Text);
    textAttributes.Text := '';
    textValues.Text := '';
  end
  else
  begin
    l := listAttributes.Items[listAttributes.ItemIndex];
    l.Caption:=textAttributes.Text;
    l.SubItems[0] := textValues.Text;
    textAttributes.Text := '';
    textValues.Text := '';
  end;
end;

procedure TfrmElement.btnClearClick(Sender: TObject);
begin
  listAttributes.ItemIndex := -1;
  textAttributes.Text := '';
  textValues.Text := '';
end;

procedure TfrmElement.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TfrmElement.btnDeleteAttributeClick(Sender: TObject);
begin
  if listAttributes.ItemIndex < 0 then exit;
  textAttributes.Text := '';
  textValues.Text := '';
  listAttributes.Items.Delete(listAttributes.ItemIndex);
end;

procedure TfrmElement.FormDestroy(Sender: TObject);
begin
  elements.Free;
  tags.Free;
end;

end.

